package com.itic.mall.system.users.giveMoney.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.UserType;
import com.itic.mall.system.inter.finance.makeMoneyLog.service.FinanceUserMakeMoneyLogService;
import com.itic.mall.system.inter.users.giveMoney.persistence.dao.UsersGiveMoneyDao;
import com.itic.mall.system.inter.users.giveMoney.persistence.model.UsersGiveMoney;
import com.itic.mall.system.inter.users.giveMoney.persistence.model.UsersGiveMoneyExample;
import com.itic.mall.system.inter.users.giveMoney.persistence.model.UsersGiveMoneyExample.Criteria;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.users.usersmall.service.UsersMallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UsersGiveMoneyService
     extends CrudService<UsersGiveMoneyDao,UsersGiveMoney,UsersGiveMoneyExample> {
    @Autowired
    private UsersGiveMoneyDao usersGiveMoneyDao;
    @Autowired
    private UsersMallService usersMallService;
    @Autowired
    private FinanceUserMakeMoneyLogService financeUserMakeMoneyLogService;

    public List<UsersGiveMoney> findMoneyByUserId(String userId){
    	UsersGiveMoneyExample example = new UsersGiveMoneyExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andUserIdEqualTo(userId);
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	return this.list(example);
    }
    
    public String addGiveMoney(String userId,double consume,double commission) {
    	UsersGiveMoney usersGiveMoney = new UsersGiveMoney();
    	usersGiveMoney.setCommission(commission);
    	usersGiveMoney.setConsume(consume);
    	usersGiveMoney.setUserId(userId);
    	return this.add(usersGiveMoney);
    }
    
    public void userRegistMoney(UsersMall usersMall,double consume,double commission) {
    	if(!UserType.isYDZ(usersMall.getType())) {
    		this.addGiveMoney(usersMall.getId(), consume, commission);
    		double livingMoney = usersMall.getLivingMoney()==null?0:usersMall.getLivingMoney();
    		livingMoney = livingMoney+commission;
    		usersMall.setLivingMoney(livingMoney);
    		this.usersMallService.edit(usersMall);
    		this.financeUserMakeMoneyLogService.addMakeMoneyLog(usersMall.getId(), commission, null);
    	}
    }
}